<template>
  <div id="login_container">
    <h1 class="login_title">welcome to shop_system</h1>
    <div class="login_box">
      <h3>欢迎登陆后台系统</h3>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="loginForm"
        :rules="loginFromRules"
        ref="loginFormRef"
      >
        <!-- 用户名区域 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            placeholder="用户名"
            v-model="loginForm.username"
            type="text"
          ></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="用户密码"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="login_btns">
          <el-button type="primary" plain @click="submitForm">登录</el-button>
          <el-button type="info" plain @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //表单登录数据
      loginForm: {
        username: "admin",
        password: "123456",
      },
      //表单验证规则
      loginFromRules: {
        //用户名验证是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 10, message: "请输入3-10个字符", trigger: "blur" },
        ],
        //用户密码验证是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 6, max: 15, message: "请输入6-15个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //点击登录按钮进行的操作
    submitForm() {
      this.$refs.loginFormRef.validate(async (valid, obj) => {
        if (valid) {
          let {
            data: { data, meta },
          } = await this.$api.isLogin(this.loginForm);
          if (meta.status !== 200) {
            this.$message.error("登录失败");
          } else if (meta.status === 200) {
            this.$message({
              duration:1000,
              type:'success',
              message:'登录成功'
            });
            /*
             登录成功之后
             1.将登录成功的token,保存到sessionStorage中
               @1.项目中除了登录之外的其他api接口,都需要登录之后才能访问
               @2.token只应在当前网页打开期间生效,所以将token保存到sessionStorage中
             2.通过编程式导航跳转到后台主页
           */
          sessionStorage.setItem('token',data.token)
          this.$router.push('/home')
          }
        }
      });
    },
    //点击重置按钮,对整个表单重置
    resetForm() {
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
#login_container {
  height: 100%;
  background: url("../assets/imgs/login_img2.jpeg") no-repeat;
  /* 背景图垂直、水平均居中 */
  background-position: center center;
  /* 让背景图基于容器大小伸缩 */
  background-size: cover;
  position: relative;
  .login_title {
    width: 450px;
    height: 50px;
    line-height: 50px;
    color: aliceblue;
    font-style: italic;
    margin-top: 0;
    margin-left: 30px;
    padding-top: 20px;
  }
  .login_box {
    width: 450px;
    height: 300px;
    border-radius: 5px;
    background-color: rgba(8, 4, 4, 0.5);
    color: white;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    h3 {
      text-align: center;
    }
    .login_form {
      padding: 0 30px;
      //margin-top: 50px;
      .login_btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
